#include <gdk/gdkkeysyms.h>
#include <string.h>
#include "sms.h"

#define SUBJECT_PATH    "/usr/share/sms/default/layout/layout-teacher-subject/"
#define SUBJECT_LAYOUT_NAME "layout-teacher-subject"

typedef struct _Icon_Sub
{
	char *name;
  GooCanvasItem *icon;
	GdkPixbuf *pic ;
	GdkPixbuf *apic;
  
  LAYOUT_WIN_ID id;

  char *cmd;
  char **argv;
  int argc;

  int x;
  int y;
  int size;
  int asize;

  GooCanvasItem *text;
  int title_size ;
  char *font ;
  char *color ;
}Icon_Sub;


typedef struct _Sub_Layout
{
  GooCanvasItem *bg;
  int micon_count;
  int focus_id ;
  Icon_Sub *icons;

  Animate *animates;
  int animate_count;

  Layout *layout;
  char buf[512];
}Sub_Layout;


static gboolean subject_layout_load_bg(xmlNode *a_node,Layout *layout)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  Sub_Layout  *sublayout = layout->private;
  GooCanvasItem *ship ,*cloud ,*logo ,*text;

  GdkPixbuf*  pixbuf;
  xmlChar * val;
  char pic[256];

  if(sublayout->bg || !a_node)
  {
    return FALSE;
  }

  val = xmlGetProp (a_node,(xmlChar*)"pic");
  if(!val)
  {
    return FALSE;
  }

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  printf("layout->name=%s\n" ,layout->name);
  pixbuf=sms_load_pic(pic,800,480);
  if(!pixbuf)
  {
    return FALSE;
  }
  sublayout->bg=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0,NULL);
  g_object_unref(pixbuf);

	//clouds
  val = xmlGetProp (a_node,(xmlChar*)"clouds");
  if(!val)
    return FALSE;

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  printf("layout->name=%s\n" ,layout->name);
  pixbuf=sms_load_pic(pic,-1,-1);
  if(!pixbuf)
    return FALSE;

  cloud = goo_canvas_image_new (layout->cavas, pixbuf, 0, 0,NULL);   
  goo_canvas_item_set_simple_transform(cloud ,84 ,40 ,1 ,0);
  g_object_unref(pixbuf);
  
	//ships
  val = xmlGetProp (a_node,(xmlChar*)"ship");
  if(!val)
    return FALSE;

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  printf("layout->name=%s\n" ,layout->name);
  pixbuf=sms_load_pic(pic,-1,-1);
  if(!pixbuf)
    return FALSE;

  ship = goo_canvas_image_new (layout->cavas, pixbuf, 0, 0,NULL);   
  goo_canvas_item_set_simple_transform(ship ,360 ,-50 ,1 ,0);
  g_object_unref(pixbuf);

	//logo
  val = xmlGetProp (a_node,(xmlChar*)"logo");
  if(!val)
    return FALSE;

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  printf("layout->name=%s\n" ,layout->name);
  pixbuf=sms_load_pic(pic,-1,-1);
  if(!pixbuf)
    return FALSE;

  logo = goo_canvas_image_new (layout->cavas, pixbuf, 0, 0,NULL);   
  goo_canvas_item_set_simple_transform(logo ,90 ,40 ,1 ,0);
  g_object_unref(pixbuf);

//text
  text = goo_canvas_text_new(layout->cavas,"科目",
      180,83,-1,GTK_ANCHOR_CENTER,
      "font", "Sans Bold black",NULL);

	return TRUE ;
}


static gboolean
on_sub_focus_in(GooCanvasItem *item,
	     GooCanvasItem *target,
	     GdkEventFocus *event,
	     gpointer data)
{
	printf("~~~%s\n" ,__FUNCTION__);
	Icon_Sub *icon = (Icon_Sub *)data ;
	gchar *id;
	id = g_object_get_data (G_OBJECT (item), "id");

	printf("x= %f ,y=%f\n" ,(gdouble)icon->x ,(gdouble)icon->y);
	g_object_set (item, "pixbuf", icon->apic  ,
																	"x" ,(gdouble)icon->x-30 ,
																	"y" ,(gdouble)icon->y-20 ,NULL);
	g_object_set(icon->text ,"text" ,id  , "fill_color" ,"red" , NULL);

	g_print ("%s received focus-in event\n", id ? id : "unknown");

	return FALSE;
}


static gboolean
on_sub_focus_out(GooCanvasItem *item,
	      GooCanvasItem *target,
	      GdkEventFocus *event,
	      gpointer data)
{
	Icon_Sub *icon = (Icon_Sub *)data ;
	gchar *id;
	id = g_object_get_data (G_OBJECT (item), "id");

	g_object_set (item, "pixbuf", icon->pic,
																	"x" ,(gdouble)icon->x ,
																	"y" ,(gdouble)icon->y ,NULL);
	g_object_set(icon->text ,"text", icon->name, "fill_color" ,icon->color ,NULL);

	g_print ("%s received focus-out event\n", id ? id : "unknown");
	return FALSE ;
}

static gboolean
on_sub_key_press(GooCanvasItem *item,
	      GooCanvasItem *target,
	      GdkEventKey *event,
	      gpointer data)
{
	Icon_Sub *icon = (Icon_Sub *)data ;
  gchar *id;

  id = g_object_get_data (G_OBJECT (item), "id");

  g_print ("%s received key-press event\n", id ? id : "unknown");

  return FALSE;
}

static void  subject_layout_create_icon(xmlNode *a_node,Layout *layout ,int id)
{
  Sub_Layout  *sublayout = layout->private;
  SMS *sms=layout->sms;
  GdkPixbuf*  pixbuf;
  xmlChar * val;
  char pic[256];
  char font[64];

  sublayout->icons = (Icon_Sub *)g_malloc0(sizeof(Icon_Sub)+1);
  
  printf("id=%d\n" ,id);
  val = xmlGetProp (a_node,(xmlChar*)"name");
  if(!val)
  {
    goto err;
  }

  sublayout->icons->name=g_strdup(val);
  sublayout->icons->size=sms_get_xml_int(a_node,(xmlChar*)"size",64);
  sublayout->icons->asize=sms_get_xml_int(a_node,(xmlChar*)"asize",sublayout->icons->size);     
  sublayout->icons->x=sms_get_xml_int(a_node,(xmlChar*)"x",sublayout->icons->x);     
  sublayout->icons->y=sms_get_xml_int(a_node,(xmlChar*)"y",sublayout->icons->y);   
  sublayout->icons->title_size = sms_get_xml_int(a_node,(xmlChar*)"title_size",sublayout->icons->title_size);


  val = xmlGetProp (a_node,(xmlChar*)"pic");
  if(!val)
  {
    goto err;
  }
  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  sublayout->icons->pic = sms_load_pic(pic,sublayout->icons->size,sublayout->icons->size);
  if(!sublayout->icons->pic)
  {
    goto err;
  }

  val = xmlGetProp (a_node,(xmlChar*)"apic");
  if(!val)
  {
    goto err;
  }
  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  sublayout->icons->apic = sms_load_pic(pic,sublayout->icons->asize,sublayout->icons->asize);
  if(!sublayout->icons->apic)
  {
    goto err;
  }
  
  sublayout->icons->icon = goo_canvas_image_new (layout->cavas, sublayout->icons->pic, 
  																																									sublayout->icons->x, sublayout->icons->y ,
  																																									"can-focus", TRUE ,NULL);	
  g_object_set_data (G_OBJECT (sublayout->icons->icon), "id", sublayout->icons->name);

  val = xmlGetProp (a_node,(xmlChar*)"color");
  if(val)
  {
    sublayout->icons->color=g_strdup(val);
  }
  val = xmlGetProp (a_node,(xmlChar*)"font");
  if(val)
  {
    sublayout->icons->font=g_strdup(val);
  }
  
	sprintf(font ,"%s %d" ,sublayout->icons->font ,sublayout->icons->title_size);
	printf("~~~%s\n" ,font);

  sublayout->icons->text = goo_canvas_text_new(layout->cavas ,sublayout->icons->name,
  																					sublayout->icons->x+35 ,sublayout->icons->y+120 ,
  																					-1 ,GTK_ANCHOR_CENTER , "fill_color" ,sublayout->icons->color ,
  																					"font", font ,NULL);

  g_signal_connect (sublayout->icons->icon, "focus_in_event",
		    G_CALLBACK (on_sub_focus_in), sublayout->icons);
  
  g_signal_connect (sublayout->icons->icon, "focus_out_event",
		    G_CALLBACK (on_sub_focus_out), sublayout->icons);

  g_signal_connect (sublayout->icons->icon, "key_press_event",
		    G_CALLBACK (on_sub_key_press), sublayout->icons);

  return ;

err:
  fprintf(stderr,"%s:error\n",__FUNCTION__);
  return ;
  
}

static gboolean subject_layout_load_icons(xmlNode *a_node,Layout *layout)
{
  Sub_Layout  *sublayout = layout->private;
  xmlNode *cur_node = NULL;
  int i=0;

  if(!a_node)
  {
    return FALSE;
  }

  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
    if (cur_node->type == XML_ELEMENT_NODE){
      if(strcmp(cur_node->name,"icon")==0){
        subject_layout_create_icon(cur_node,layout ,i);
        i++;
      }
    }
  }
  printf("i = %d \n" ,i);
  sublayout->micon_count = i ;

  return TRUE ;
}

#define ANIMATE_FREEZE   1
#define ANIMATE_RESET    2
#define ANIMATE_RESTART  3
#define ANIMATE_BOUNCE   4

static Animate * subject_layout_create_animate(xmlNode *a_node,Layout *layout)
{
  printf("~~~%s\n" ,__FUNCTION__);

  Sub_Layout  *sublayout = layout->private;
  Animate *animate;
  xmlChar * val;
  char pic[256];

  gdouble start_x;
  gdouble start_y;
  gdouble stop_x;
  gdouble stop_y;

  gdouble init_scale;
  gdouble init_rotation;
  gdouble animate_scale;
  gdouble animate_rotation;
  gboolean isAbsolute;
  gint total_time;
  gint step_time;
  GooCanvasAnimateType type;

  start_x = sms_get_xml_double (a_node, (xmlChar*)"start_x", 0);
  start_y = sms_get_xml_double (a_node, (xmlChar*)"start_y", 0);
  stop_x = sms_get_xml_double (a_node, (xmlChar*)"stop_x", 0);
  stop_y = sms_get_xml_double (a_node, (xmlChar*)"stop_y", 0);
  init_scale = sms_get_xml_double (a_node, (xmlChar*)"init_scale", 0);
  init_rotation = sms_get_xml_double (a_node, (xmlChar*)"init_rotation", 0);
  animate_scale = sms_get_xml_double (a_node, (xmlChar*)"animate_scale", 0);
  animate_rotation = sms_get_xml_double (a_node, (xmlChar*)"animate_rotation"
, 0);
  isAbsolute = sms_get_xml_boolean (a_node, (xmlChar*)"isAbsolute");
  total_time = sms_get_xml_int (a_node, (xmlChar*)"total_time", 0);
  step_time = sms_get_xml_int (a_node, (xmlChar*)"step_time", 0);

  switch (sms_get_xml_int (a_node, (xmlChar*)"type", 1))
  {
    case ANIMATE_FREEZE:
      type = GOO_CANVAS_ANIMATE_FREEZE;
      break;
    case ANIMATE_RESET:
      type = GOO_CANVAS_ANIMATE_RESET;
      break;
    case ANIMATE_RESTART:
      type = GOO_CANVAS_ANIMATE_RESTART;
      break;
    case ANIMATE_BOUNCE:
      type = GOO_CANVAS_ANIMATE_BOUNCE;
      break;
    default: break;
  }
  g_print ("startx: %f, starty: %f, stopx: %f, stopy: %f,init_scale: %f, animate_scale: %f, animate_rotation: %f, total_time: %d, step_time %d\n",start_x, start_y, stop_x, stop_y, init_scale, animate_scale, total_time, step_time);

  val = xmlGetProp (a_node,(xmlChar*)"pic");
  if(!val)
    goto err;
  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout
->name,val);
  g_print ("pic path: %s", pic);

  animate = animate_new (layout->cavas, pic, start_x, start_y, stop_x, stop_y
, init_scale, init_rotation,
      animate_scale, animate_rotation, isAbsolute, total_time, step_time, type
);
  return animate;
err:
  fprintf(stderr,"%s:error\n",__FUNCTION__);
  return NULL;
}

static gboolean subject_layout_load_animates(xmlNode *a_node, Layout *layout)
{
  Sub_Layout  *sublayout = layout->private;

  xmlNode *cur_node = NULL;
  Animate * animate;

  if(!a_node)
    return FALSE;
  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
    if (cur_node->type == XML_ELEMENT_NODE) {
      if(strcmp(cur_node->name,"animate")==0)
      {
        animate = subject_layout_create_animate(cur_node, layout);
        if(animate)
        {
          sublayout->animate_count++;
          if(sublayout->animates)
          {
            sublayout->animates->next = animate;
          }
          else
          {
            sublayout->animates = animate;
          }
        }
      }
    }
  }
  return TRUE;
}

static gboolean subject_layout_travel_xml(xmlNode *a_node,Layout *layout)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  Sub_Layout  *sublayout = layout->private;
  xmlNode *cur_node = NULL;
  xmlNode *bg_node = NULL;
  xmlNode *icon_node = NULL;
  xmlNode *animate_node = NULL;

  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
    if (cur_node->type == XML_ELEMENT_NODE){
      printf("node type: Element, name: %s\n", cur_node->name);
      if(strcmp(cur_node->name,"bg")==0)
        bg_node=cur_node;
      else if(strcmp(cur_node->name,"icons")==0)
        icon_node=cur_node;
      else if(strcmp(cur_node->name,"animates")==0)
      		animate_node = cur_node;
    }
  }

  if(!bg_node||!icon_node ||!animate_node)
    return FALSE;

  subject_layout_load_bg(bg_node,layout);
  subject_layout_load_animates (animate_node->children, layout);
  subject_layout_load_icons(icon_node->children,layout);

  if(sublayout->micon_count>=10)
    return TRUE;

  return FALSE;
}

gboolean subject_layout_load_config(Layout *layout)
{
  gboolean ret=FALSE;
  xmlDoc *doc = NULL;
  xmlNode *root_element = NULL;
  Sub_Layout  *sublayout = layout->private;
  char conf[256];
  sprintf(conf,"%s/%s/layout/%s/layout.xml",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name);
  printf("layout->name=%s\n" ,layout->name);

  doc = xmlReadFile(conf, NULL, 0);
  if (doc == NULL) {
    fprintf(stderr,"error: could not parse file %s\n", conf);
    return ret;
  }
  root_element = xmlDocGetRootElement(doc);

  if(root_element)
  {
    xmlNode *cur_node = NULL;
    for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
      if (cur_node->type == XML_ELEMENT_NODE) {
        ret=TRUE;
        break;
      }
    }
    if(ret)
      ret=subject_layout_travel_xml(cur_node->children,layout);
  }

  xmlFreeDoc(doc);
  xmlCleanupParser();
  return ret;
}

static gboolean subject_layout_key_press(Layout * layout,GdkEventKey *event)
{
  Sub_Layout  *sublayout = layout->private;
  int i;
  if(event->keyval == GDK_Escape)
  {		
    sms_change_win_to(layout->sms,LAYOUT_WIN_TEACHER);
    return TRUE;
  }
  return FALSE ;
}

static gboolean subject_layout_clear(Layout *layout,gpointer data)
{
	printf("call %s\n",__FUNCTION__);
	Sub_Layout  *sublayout = layout->private ;
	int i = 0;

	g_object_unref(sublayout->bg);
	g_free(sublayout);
	return TRUE ;
}
#if 0
void subject_layout_reset_icons(Layout *layout)
{
	Sub_Layout  *sublayout = layout->private ;
	gchar *id;
	id = g_object_get_data (G_OBJECT (sublayout->icons->icon), "id");
	g_object_set (sublayout->icons->icon, "pixbuf", sublayout->icons->apic ,NULL);
	g_object_set(sublayout->icons->text ,"text" ,id  , "fill_color" ,"red" , NULL);
}
#endif

static gboolean subject_layout_init(Layout *layout,gpointer data)
{
  printf("call %s\n",__FUNCTION__);

  Sub_Layout  *sublayout=(Sub_Layout *)g_malloc0(sizeof(Sub_Layout)+1);
  layout->private=(void *)sublayout;

  layout->cavas = goo_canvas_group_new(layout->parent,NULL);	

  if(!subject_layout_load_config(layout))
  {
    fprintf(stderr,"%s:load config err\n",__FUNCTION__);
    //return FALSE;//exit(-1);
  }
  //subject_layout_reset_icons(layout);

  return TRUE;
}


Layout *subject_layout_new(GooCanvasItem * parent,gpointer sms)
{
  Layout *layout=layout_new( parent,LAYOUT_TYPE_WIN ,LAYOUT_WIN_TEACHER_SUBJECT,
      SUBJECT_LAYOUT_NAME,sms);

  layout_set_func(layout,"init",subject_layout_init);
  layout_set_func(layout,"clear",subject_layout_clear);
  layout_set_func(layout,"key-press",subject_layout_key_press);

  return layout;
}


